package cn.edu.ujn.ch17.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.ujn.ch17.dao.Customer;
import cn.edu.ujn.ch17.service.ICustomerService;

@Controller
public class CustomerController {
	@Autowired
	private ICustomerService customerService;
	@GetMapping("/addCustomer")
	public String toAdd() {
		return "customer/add";
	}
	@PostMapping("/addCustomer")
	public String add(Customer customer) {
		int insert = this.customerService.insert(customer);
		return "success";
	}
	@GetMapping("/detail")
	public String toDetail() {
		return "customer/detail";
	}
	@GetMapping("/customer/{id}")
	@ResponseBody
	public Customer find(@PathVariable("id") Integer id) {
		Customer customer = this.customerService.selectByPrimaryKey(id);
		return customer;
	}
	@GetMapping("/listCustomer")
	public String list(Model model) {
		List<Customer> list = this.customerService.selectAll();
		model.addAttribute("list",list);
		return "customer/list";
	}
	
}
